#!/usr/bin/python
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
#cron:0 19 * * *
import datetime
import time
import requests
import re
import json
import random
from bs4 import BeautifulSoup
import notify2
import os

def get_account_info_from_env():
    env_account_info = os.environ.get('meiriyibo')
    if env_account_info:
        account, password = env_account_info.split('&')
        return account, password
    else:
        print("环境变量 'env' 未设置或格式不正确，请检查！")
        return None, None
account, password = get_account_info_from_env()
headers = {
'Host':'xdl.qmxyls.com',
'Connection':'keep-alive',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0(Linux;Android11;M2104K10ACBuild/RP1A.200720.011;wv)AppleWebKit/537.36(KHTML,likeGecko)Version/4.0Chrome/86.0.4240.99XWEB/3165MMWEBSDK/20210902MobileSafari/537.36MMWEBID/1314MicroMessenger/8.0.15.2020(0x28000F31)Process/toolsmpWeChat/arm64WeixinNetType/WIFILanguage/zh_CNABI/arm64',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/wxpic,image/tpg,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'X-Requested-With':'com.tencent.mm',
'Accept-Encoding':'gzip,deflate',
'Accept-Language':'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
'Cookie':'PHPSESSID=a5b5ujbtasc42fds7r8tls01mh; path=/, keeplogin=1312%7C86400%7C1639367662%7C443dc52456e8b98bd1af1991b4b86c90; expires=Sun, 19-Dec-2021 03:54:22 GMT; Max-Age=604800; path=/',
}
headers1={
#'POSThttp':'//xdl.qmxyls.com/vykgcqpiVo.php/index/loginHTTP/1.1',
'Host':'xdl.qmxyls.com',
'Connection':'keep-alive',
'Content-Length':'91',
'Accept':'application/json,text/javascript,*/*;q=0.01',
'X-Requested-With':'XMLHttpRequest',
'User-Agent':'Mozilla/5.0(WindowsNT10.0;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/86.0.4240.198Safari/537.36',
'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8',
'Origin':'http://xdl.qmxyls.com',
'Accept-Encoding':'gzip,deflate',
'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8',
'Cookie':'',

}

#__token__=bb2ff1de36e4aa86794adb963f697be2&username=18954511037&password=123456&keeplogin=1
def lin(account, password):
 #requests.get(f"http://pushplus.hxtrip.com/send?token=c965d00470284ef785662292022b8455&title=校园安全每日签到&content={tui}&template=html")
    #url = 
    url = f'http://xdl.qmxyls.com/vykgcqpiVo.php/ring/agetn_log/index?addtabs=1'
    url1 = f'http://xdl.qmxyls.com/vykgcqpiVo.php/index/login'
    params1 = {"addtabs":"1"}
    data = {"__token__":"bb2ff1de36e4aa86794adb963f697be2","username":account,"password":password,"keeplogin":"1"}
    #a = requests.post(url=url1,headers=headers1,data=data)
    #r = requests.get(url,headers=headers1)
    a = requests.post(url=url1,headers=headers1,data=data)
    a1 = a.text
    #tui1 = r.text
    b = a.headers['Set-Cookie']
    headers2 = {
    'Host':'xdl.qmxyls.com',
    'Connection':'keep-alive',
    'Upgrade-Insecure-Requests':'1',
    'User-Agent':'Mozilla/5.0(Linux;Android11;M2104K10ACBuild/RP1A.200720.011;wv)AppleWebKit/537.36(KHTML,likeGecko)Version/4.0Chrome/86.0.4240.99XWEB/3165MMWEBSDK/20210902MobileSafari/537.36MMWEBID/1314MicroMessenger/8.0.15.2020(0x28000F31)Process/toolsmpWeChat/arm64WeixinNetType/WIFILanguage/zh_CNABI/arm64',
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/wxpic,image/tpg,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'X-Requested-With':'com.tencent.mm',
    'Accept-Encoding':'gzip,deflate',
    'Accept-Language':'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'Cookie':f'{b}',
    }
    #r = requests.get(url,headers=headers)
    data1 = {'addtabs':'1','sort':'id','order':'desc','offset':'0','limit':'10','filter':'%7B%7D','op':'%7B%7D','_':'1639232821473'}
    url2 = f'http://xdl.qmxyls.com/vykgcqpiVo.php/ring/agetn_log/index'
    url3 = f'http://xdl.qmxyls.com/vykgcqpiVo.php/ring/agetn_log/index?addtabs=1&amp;sort=id&amp;order=desc&amp;offset=0&amp;limit=10&amp;filter=%7B%7D&amp;op=%7B%7D&amp;_=1639234618618'
    res = requests.get(url=url3,headers=headers2)
    html6 = res.text
    print(res.text)
    #print(a.headers)
    #print(a.text)
    #print(b)
    #print(r.text)
    tui = res.text
    # 使用BeautifulSoup解析HTML
    return html6
soup = BeautifulSoup(lin(account, password), 'html.parser')

# 提取剩余时间
remaining_time_element = soup.find('div', class_='col-xs-6 col-sm-3').find('h3', class_='text-blue')
remaining_time = remaining_time_element.text.strip() if remaining_time_element else None

# 提取今日打铃次数
today_bell_element = soup.find('div', class_='col-xs-6 col-sm-4').find_all('h3', class_='text-blue')[0]
today_bell_count = today_bell_element.text.strip() if today_bell_element else None

# 提取本月打铃次数
this_month_bell_element = soup.find('div', class_='col-xs-6 col-sm-4').find_all('h3', class_='text-blue')[1]
this_month_bell_count = this_month_bell_element.text.strip() if this_month_bell_element else None

# 提取总打铃次数
total_bell_element = soup.find('div', class_='col-xs-6 col-sm-4').find_all('h3', class_='text-blue')[2]
total_bell_count = total_bell_element.text.strip() if total_bell_element else None

print("剩余时间：", remaining_time)
print("今日打铃次数：", today_bell_count)
print("本月打铃次数：", this_month_bell_count)
print("总打铃次数：", total_bell_count)
contents = f'Token有效期：{remaining_time}\n今日打铃次数：{today_bell_count}\n本月打铃次数：{this_month_bell_count}\n总打铃次数：{total_bell_count}次'

title='每日一播打铃系统统计'
notify2.send(title, contents)